package com.tro.mail.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.tro.mail.dao.entity.MailPabTable;
import com.tro.mail.dao.entity.MailPabTableExample;
import com.tro.mail.dao.mapper.MailPabTableMapper;
import com.tro.mail.util.EasyuiTree;

/**
 * 个人通讯录控制器
 * */
@Controller
@RequestMapping("/pab")
public class PAbController {
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	MailPabTableMapper mailpabmanager;
	
	/**
	 * 初始化个人通讯录
	 * */
	@RequestMapping("getPab")
	@ResponseBody
	public String getPab(String id,HttpSession session){
		List<EasyuiTree> tlist=new ArrayList<EasyuiTree>();
		EasyuiTree tree=new EasyuiTree();
		if(StringUtils.isBlank(id)){//第一次加载
			 tree.setId("001");
			 tree.setText("所有分组");
			 tree.setIconCls("icon-hamburg-category");
			 //查找出所有第一层级
			 List<EasyuiTree> children=new ArrayList<EasyuiTree>();
			 MailPabTableExample me=new MailPabTableExample();
			 me.createCriteria().andGroupCodeEqualTo("001")
			 .andLoginUserEqualTo(session.getAttribute("MAIL_USER_ID").toString());
			 List<MailPabTable> mt=mailpabmanager.selectByExample(me);
			 for(MailPabTable i:mt){
				 EasyuiTree tree1=new EasyuiTree();
				 tree1.setId(i.getId().toString());
				 if("1".equals(i.getPerson())){//为人员
					 tree1.setIconCls("icon-hamburg-hire-me");
					 tree1.setText(i.getGroupName()+" &lt; "+i.getEmail()+" &gt;");
					 tree1.setIsleaf(false); 
				 }else{
					 tree1.setIconCls("icon-hamburg-category");
					 tree1.setText(i.getGroupName());
					 tree1.setState("closed");
				 }
				 children.add(tree1);
			 }
			 tree.setChildren(children);
			 tlist.add(tree);
		}else{//第二次进入
			List<EasyuiTree> children=new ArrayList<EasyuiTree>();
			 MailPabTableExample me=new MailPabTableExample();
			 me.createCriteria().andGroupCodeEqualTo(id)
			 .andLoginUserEqualTo(session.getAttribute("MAIL_USER_ID").toString());
			 List<MailPabTable> mt=mailpabmanager.selectByExample(me);
			 for(MailPabTable i:mt){
				 EasyuiTree tree1=new EasyuiTree();
				 tree1.setId(i.getId().toString());
				 if("1".equals(i.getPerson())){//为人员
					 tree1.setIconCls("icon-hamburg-hire-me");
					 tree1.setText(i.getGroupName()+" &lt; "+i.getEmail()+" &gt;");
					 tree1.setIsleaf(false); 
				 }else{
					 tree1.setIconCls("icon-hamburg-category");
					 tree1.setText(i.getGroupName());
					 tree1.setState("closed");
				 }
				 children.add(tree1);
			 }
			 tlist=children;
		}
		return JSON.toJSONString(tlist);
	}
	
	
	/**
	 * 树加载，一次性加载完成
	 * */
	@RequestMapping("getPabInit")
	@ResponseBody
	public String getPabInit(String id,HttpSession session){
		String userId=session.getAttribute("MAIL_USER_ID").toString();
		List<EasyuiTree> tlist=new ArrayList<EasyuiTree>();
		EasyuiTree tree=new EasyuiTree();
		tree.setId("001");
		tree.setText("所有分组");
		tree.setIconCls("icon-hamburg-category");
		List<EasyuiTree> children=new ArrayList<EasyuiTree>();
		MailPabTableExample me=new MailPabTableExample();
		me.createCriteria().andGroupCodeEqualTo("001")
		 .andLoginUserEqualTo(userId);
		MailPabTableExample me1=new MailPabTableExample();
		me1.createCriteria().andLoginUserEqualTo(userId);
		 List<MailPabTable> mt=mailpabmanager.selectByExample(me);
		 List<MailPabTable> mt1=mailpabmanager.selectByExample(me1);
		 for(MailPabTable i:mt){
			 EasyuiTree tree1=new EasyuiTree();
			 tree1.setId(i.getId().toString());
			 if("1".equals(i.getPerson())){//为人员
				 tree1.setIconCls("icon-hamburg-hire-me");
				 tree1.setText(i.getGroupName()+" &lt; "+i.getEmail()+" &gt;");
				 tree1.setIsleaf(false); 
			 }else{
				 tree1.setIconCls("icon-hamburg-category");
				 tree1.setText(i.getGroupName());
				 tree1.setState("closed");
				 //查询出下面的子节点
				 tree1.setChildren(createComboTreeChildren(mt1,i.getId().toString(),userId));
			 }
			 children.add(tree1);
		 }
		tree.setChildren(children);
		tlist.add(tree);
		return JSON.toJSONString(tlist);
	}
	 
	
	/**
	 * 递归加载子节点
	 * */
	private List<EasyuiTree> createComboTreeChildren( List<MailPabTable> list, String fid,String userId) {  
	    List<EasyuiTree> childList = new ArrayList<EasyuiTree>();  
	    for (int j = 0; j < list.size(); j++) {  
	    	EasyuiTree tree = null;  
	    	MailPabTable treeChild = (MailPabTable) list.get(j);  
	        if (treeChild.getGroupCode().equals(fid)) {  
	        	tree = new EasyuiTree();  
	        	tree.setId(list.get(j).getId().toString());  
	        	tree.setText(list.get(j).getGroupName());  
	        	if("1".equals(treeChild.getPerson())){//为人员
	        		 tree.setIconCls("icon-hamburg-hire-me");
					 tree.setText(treeChild.getGroupName()+" &lt; "+treeChild.getEmail()+" &gt;");
					 tree.setIsleaf(false); 
	        	}else{
	        		 tree.setIconCls("icon-hamburg-category");
					 tree.setText(treeChild.getGroupName());
					 tree.setState("closed");
					 MailPabTableExample me2=new MailPabTableExample();
						me2.createCriteria().andLoginUserEqualTo(userId).andGroupCodeEqualTo(tree.getId());
					 if((long)mailpabmanager.countByExample(me2)>0){
						 tree.setChildren(createComboTreeChildren(list, tree.getId(),userId));  
					 }else{
						 tree=null;
					 }
	        	}
	        }  
	        
	        if (tree != null){  
	            childList.add(tree);
	        }
	    }  
	    return childList;  
	}  
	
	
	
	/**
	 * 查询
	 * */
	 @ResponseBody
	 @RequestMapping("search")
	 public String search(String query,HttpSession session){
		 MailPabTableExample me=new MailPabTableExample();
		 me.createCriteria().andGroupNameLike(""+query+"%")
		 .andLoginUserEqualTo(session.getAttribute("MAIL_USER_ID").toString());
		 List<MailPabTable> list =mailpabmanager.selectByExample(me);
		 return JSON.toJSONString(list);
	 }
	
}
